import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.concurrent.TimeUnit;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.junit.*;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
//import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.support.ui.Select;

//This is a driver script
//Dependencies: xl.java and Webmethods.java
public class KDF4 {

	WebDriver myFD = new FirefoxDriver();
	
	String vKW, vxPath, vTestData;
	@Test
	public void myDriver() throws Exception{
	/*	System.setProperty("webdriver.ie.driver", "D:\\Downloads\\IEDriverServer.exe");
		WebDriver myFD = new InternetExplorerDriver();*/
			
		myFD.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
		//Read the TCs and TSs
		String[][] xTC, xTS;
		xTC = xl.readXL("D:/A16/Excel/KDF Automation.xls", "Test Cases");
		xTS = xl.readXL("D:/A16/Excel/KDF Automation.xls", "Test Steps");
		String xlResults = "D:/A16/Excel/KDF Automation_results3.xls";
		//Go to each row in the test case and identify which one to execute
		for(int i=1; i<xTC.length; i++){
			if(xTC[i][3].equalsIgnoreCase("Y")){
					for (int k=1; k<xTS.length; k++){
						if (xTC[i][0].equalsIgnoreCase(xTS[k][1])){
						
							vKW = xTS[k][4];
							vxPath = xTS[k][5];
							vTestData = xTS[k][6];
							String vError = "Pass";
							try {
								
							//Call the corresponding keyword functions
							if(vKW.equalsIgnoreCase("enterText")){
								Webmethods.enterText(myFD, vxPath, vTestData);
							}
							if(vKW.equalsIgnoreCase("enterTextbyid")){
								Webmethods.enterTextbyid(myFD, vxPath, vTestData);
							}
							if(vKW.equalsIgnoreCase("navigateBrowser")){
								Webmethods.navigateBrowser(myFD, vTestData);
							}
							if(vKW.equalsIgnoreCase("clickElementbyid")){
								Webmethods.clickElementbyid(myFD, vxPath);
							}
							if(vKW.equalsIgnoreCase("clickElement")){
								Webmethods.clickElement(myFD, vxPath);
							}
							if(vKW.equalsIgnoreCase("selectList")){
								Webmethods.selectList(myFD, vxPath, vTestData);
							}
							if(vKW.equalsIgnoreCase("checkCheckbox")){
								Webmethods.checkCheckbox(myFD, vxPath);
							}
							if(vKW.equalsIgnoreCase("uncheckCheckbox")){
								Webmethods.uncheckCheckbox(myFD, vxPath);
							}
							if(vKW.equalsIgnoreCase("readText")){
								vError = Webmethods.readText(myFD, vxPath);
								System.out.println("Test Step is: " + vError);
								
							}
							if(vKW.equalsIgnoreCase("verifyText")){
								vError = Webmethods.verifyText(myFD, vxPath, vTestData);
								System.out.println("Test Step is: " + vError);
							}
							if(vKW.equalsIgnoreCase("verifyTextbycss")){
								vError = Webmethods.verifyTextbycss(myFD, vxPath, vTestData);
								System.out.println("Test Step is: " + vError);
							}
							if(vKW.equalsIgnoreCase("verifyTextbyid")){
								vError = Webmethods.verifyTextbycss(myFD, vxPath, vTestData);
								System.out.println("Test Step is: " + vError);
							}
							if(vError.equalsIgnoreCase("Pass")){
								xTS[k][8] = "Pass";	
							}else
							{
								xTS[k][8] = "Fail";
								xTS[k][9] = "Error happened as the function returned Fail";
							}
							
							
							} catch(Exception e){
								System.out.println("Error has happened " + e);
								xTS[k][8] = "Fail";
								xTS[k][9] = "Error has happened " + e;
							
							}
						}
					}
				
				
				} else {
					System.out.println("The test case " + i + " is ignored");
				}
		}
		//Write back the test status and error details into test step sheet
		xl.writeXL(xlResults, "Test Results", xTS);
		myFD.quit();
	}
}